package edu.ctbu.insurance.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import edu.ctbu.insurance.aop.PermissionVerification;
import edu.ctbu.insurance.bean.Policy;
import edu.ctbu.insurance.service.PolicyService;
import edu.ctbu.insurance.util.ResultEnum;
import edu.ctbu.insurance.util.ResultUtils;
import edu.ctbu.insurance.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-06-28
 */
@Controller
@RequestMapping("/policy")
public class PolicyController {
    @Autowired
    PolicyService policyService;

    @RequestMapping("/findAllPolicy")
    @ResponseBody
    public List<Policy> findAllPolicy(String likeProductName,String likeCustomerName,String likePolicyType){
        return policyService.findPolicysByLike(likeProductName,likeCustomerName,likePolicyType);
    }

    @RequestMapping("/findAllPolicyByPage")
    @ResponseBody
    public PageInfo findAllPolicyByPage(
            @RequestParam(defaultValue = "")String likePolicyType
            ,@RequestParam(defaultValue = "1") int pageNum
            ,@RequestParam(defaultValue = "10") int pageSize
            ,@RequestParam(defaultValue = "")String likeProductName
            ,@RequestParam(defaultValue = "")String likeCustomerName){
        return  new PageInfo(policyService.findPolicysPageByLike(likePolicyType,pageNum,pageSize,likeProductName,likeCustomerName));
    }

    @RequestMapping("/updatePolicy")
    @ResponseBody
    public String updatePolicy(Policy policy){
        policyService.updateById(policy);
        return "更新成功";
    }

    @RequestMapping("/insertPolicy")
    @ResponseBody
    public String insertPolicy(Policy policy){
        policyService.save(policy);
        return "插入成功";
    }

    @PermissionVerification(roleList = {"管理员"})
    @RequestMapping("/deleteByPolicyId")
    @ResponseBody
    public Result<String> deleteByPolicyId(Policy policy){
        policy.setDeleteFlag(0);
        policyService.updateById(policy);
        return ResultUtils.success("删除成功");
    }

    @RequestMapping("/updatePolicyStatus")
    @ResponseBody
    public int updatePolicyStatus(){
        return policyService.updatePolicyStatus();
    }

    @RequestMapping("/getProductStatistics")
    @ResponseBody
    public List<Object> getProductStatistics(){
        return policyService.getProductStatistics();
    }

    @RequestMapping("/getAddressStatistics")
    @ResponseBody
    public List<Object> getAddressStatistics(){
        return policyService.getAddressStatistics();
    }

    @RequestMapping("/getCustomerStatistics")
    @ResponseBody
    public List<Object> getCustomerStatistics(){
        return policyService.getCustomerStatistics();
    }

}
